package carcassonne;

import carcassonne.server.http.HttpChannelInitializer;
import carcassonne.server.websocket.WebsocketChannelInitializer;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class Main {

    public static void main(String[] args) {

        NioEventLoopGroup mainLoopGroup = new NioEventLoopGroup();
        NioEventLoopGroup httpLoopGroup = new NioEventLoopGroup();
        NioEventLoopGroup websocketLoopGroup = new NioEventLoopGroup();

        System.out.println("carcassonne server main...");

        try {
            ServerBootstrap httpBootstrap = new ServerBootstrap();
            httpBootstrap.group(mainLoopGroup, httpLoopGroup).channel(NioServerSocketChannel.class)
                    .childHandler(new HttpChannelInitializer());

            Channel http = httpBootstrap.bind(9101).sync().channel();

            ServerBootstrap websocketBootstrap = new ServerBootstrap();
            websocketBootstrap.group(mainLoopGroup, websocketLoopGroup).channel(NioServerSocketChannel.class)
                    .childHandler(new WebsocketChannelInitializer());

            Channel websocket = websocketBootstrap.bind(9102).sync().channel();

            http.closeFuture().sync();
            websocket.closeFuture().sync();
        } catch (Exception e) {
            e.printStackTrace();
            httpLoopGroup.shutdownGracefully();
            websocketLoopGroup.shutdownGracefully();
            mainLoopGroup.shutdownGracefully();
        }
    }
}
